package com.haoniu.web.controller.module;

import com.haoniu.common.support.Convert;
import com.haoniu.framework.util.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.haoniu.common.annotation.Log;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.module.domain.database.Banner;
import com.haoniu.module.service.BannerService;
import com.haoniu.web.core.base.BaseController;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.common.base.AjaxResult;

import java.util.List;
/**
 * 商品轮播图 信息操作处理
 *
 * @author houyijun
 * @date 2019-01-25
 */
@Controller
@RequestMapping("/module/banner")
public class BannerController extends BaseController
{
    private String prefix = "module/banner";

	@Autowired
	private BannerService bannerService;

	@RequiresPermissions("module:banner:view")
	@GetMapping()
	public String banner()
	{
	    return prefix + "/banner";
	}

	/**
	 * 查询商品轮播图列表
	 */
	@RequiresPermissions("module:banner:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Banner queryBanner) {
		startPage();
		List<Banner> list = bannerService.selectBannerList(queryBanner);
		for (Banner banner:list) {
			String imgUrl = banner.getImgUrl();
			if(StringUtils.isNotBlank(imgUrl)){
				String[] imgArray = Convert.toStrArray(imgUrl);
				banner.setImgUrl(imgArray[0]);
			}
		}
		return getDataTable(list);
	}

	/**
	 * 新增商品轮播图
	 */
	@GetMapping("/add")
	public String add() {

		return prefix + "/add";
	}

	/**
	 * 新增保存商品轮播图
	 */
	@RequiresPermissions("module:banner:add")
	@Log(title = "商品轮播图", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Banner banner) {
        banner.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(bannerService.insertBanner(banner));
	}

	/**
	 * 修改商品轮播图
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		Banner banner = bannerService.selectBannerById(id);
		mmap.put("banner", banner);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存商品轮播图
	 */
	@RequiresPermissions("module:banner:edit")
	@Log(title = "商品轮播图", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Banner banner) {
        banner.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(bannerService.updateBanner(banner));
	}

	/**
	 * 删除商品轮播图
	 */
	@RequiresPermissions("module:banner:remove")
	@Log(title = "商品轮播图", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(bannerService.deleteBannerByIds(ids));
	}

}
